package py.com.fms.ven.persistence.services;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import com.ibatis.sqlmap.client.SqlMapClient;

import py.com.fms.exception.CommonException;
import py.com.fms.persistence.services.FmsServices;
import py.com.fms.ven.bean.PricesListHeadBean;
import py.com.fms.ven.bean.PricesListDetBean;

public class PricesListServices extends FmsServices  {

	public PricesListServices(SqlMapClient client) {
		super(client);
		// TODO Auto-generated constructor stub
	}
	
	/**
	 [[[
	 [[ The following method retrieve a list of all records
	 [[ Normally used to load a grid.
	 */
	
	@SuppressWarnings("unchecked")
	public List<PricesListHeadBean> getAllPricesListHead() throws CommonException{
		List<PricesListHeadBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]]
			
			queryForList = (List<PricesListHeadBean>)this.getConfig().queryForList("pricesList.getAllPricesListHead");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PricesListDetBean> getAllPricesListDet() throws CommonException{
		List<PricesListDetBean> queryForList;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]]
			
			queryForList = (List<PricesListDetBean>)this.getConfig().queryForList("pricesList.getAllPricesListDet");
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<PricesListDetBean> getPricesListDetByHeadId(Long idHead) throws CommonException{
		List<PricesListDetBean> queryForList;
		
		HashMap<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idHead", idHead);
		
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]]
			
			queryForList = (List<PricesListDetBean>)this.getConfig().queryForList("pricesList.getPricesListDetByHeadId", parameters);
			return queryForList;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	
	/**
	 [[[
	 [[ The following methods retrieve a Bean from a determinated Id
	 */
	
	
	public PricesListHeadBean getPricesListHeadData(Long id) throws CommonException{
		PricesListHeadBean queryForObject;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForObject = (PricesListHeadBean)this.getConfig().queryForObject("pricesList.getPricesListHeadData", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	public PricesListDetBean getPricesListDetData(Long id) throws CommonException{
		PricesListDetBean queryForObject;
		try {
			
			//[[ queryForList is an implemented interface of iBatis ]] 
			
			queryForObject = (PricesListDetBean)this.getConfig().queryForObject("pricesList.getPricesListDetData", id);
			return queryForObject;
			
		} catch (SQLException e) {
			throw new CommonException(e);
		}
	}
	
	/**
	 [[[
	 [[ The following method store a bean (record) in the database.
	 */
	
	public void saveNewPricesListHead(PricesListHeadBean pricesListHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("pricesList.insPricesListHead", pricesListHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void saveNewPricesListDet(PricesListDetBean pricesListDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().insert("pricesList.insPricesListDet", pricesListDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method delete a record from the database.
	 */
	
	public void deletePricesListHeadById(Long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("id", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("pricesList.delPricesListHeadById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void deletePricesListDetById(Long id) throws CommonException{
		try {
			HashMap<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("id", id);
			this.getConfig().startTransaction();
			this.getConfig().delete("pricesList.delPricesListDetById", parameters);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	/**
	 [[[
	 [[ The following method update a record from a bean.
	 */
	public void updatePricesListHeadById(PricesListHeadBean pricesListHead) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("pricesList.updPricesListHeadById", pricesListHead);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	
	public void updatePricesListDetById(PricesListDetBean pricesListDet) throws CommonException{
		try {
			this.getConfig().startTransaction();
			this.getConfig().update("pricesList.updPricesListDetById", pricesListDet);
			this.getConfig().commitTransaction();
		} catch (SQLException e) {
			throw new CommonException(e);
		} finally {
			try {
				this.getConfig().endTransaction();
			} catch (SQLException e) {
				e.printStackTrace();
				throw new CommonException(e);
			}
		}
	}
	


}
